#include "userMgr.h"
#include "serverConn.h"
#include "user.h"


UserMgr* UserMgr::inst = 0;
UserMgr* UserMgr::Inst(){ if (inst == 0){ inst= new UserMgr(); } return inst; }
UserMgr::UserMgr():_usr(new User){

}
UserMgr::~UserMgr(){
    delete _usr;
    _conns.clear();
    //for( size_t i =0 ; i < _conns.size(); ++i){
    //_conns[i];
    //}
}
void UserMgr::sendMsg( uint32_t connID , MsgInfo* rsp){ 
    printf("send connID: %d, msginfo num: %d, msgtype: %d\n", connID, rsp->info.size(), rsp->msgType);
    _conns[connID]->sendMsg( rsp); 
}

uint32_t UserMgr::onMsgReq(MsgInfo *msg,uint32_t connID){
    printf("recieve connID: %d, msginfo num: %d, msgtype: %d\n", connID, msg->info.size(), msg->msgType);
    return _usr->onMsgReq( msg, connID);
}
void UserMgr::unregConn( ServerConn* conn){
    for ( int i = 0 ; i < _conns.size() ; ++i ){
        if ( _conns[i] == conn ){
            _conns[i] = 0;
            printf("unregister conn id: %d\n", i);
        }

    }
}
int UserMgr::regConn( ServerConn* conn){
    int r = -1;
    for ( int i = 0 ; i < _conns.size() ; ++i ){
        if ( _conns[i] == conn ){
            r = i;
        }

        if ( _conns[i] == 0){
            _conns[i] = conn;
            r= i;
        }
    }
    if ( r == -1 ){
        _conns.push_back( conn);
        r = _conns.size()-1;
    }

    printf("register conn id: %d\n", r);
    return r;
}
